package cn.ipanel.bigdata.boot.source

import cn.ipanel.bigdata.boot.Session
import cn.ipanel.bigdata.utils.Util.{isEmpty, nonEmpty}

/**
 * Author: lzz
 * Date: 2021/11/16 16:15
 */
trait Mapper extends Session with Serializable {

  def getSourceGenre: DataSource.Genre
  def getSourceModel: DataSource.Model

  def sqlToWhereOrLong(columnName: String, values: Long*): String = {
    if (nonEmpty(columnName) && nonEmpty(values)) {
      var value = ""
      values.foreach(v => {
        if (isEmpty(value)) value = s"$columnName=$v"
        else value += s" or $columnName=$v"
      })
      value
    } else "1=1"
  }

  def sqlToWhereOrInt(columnName: String, values: Int*): String = {
    if (nonEmpty(columnName) && nonEmpty(values)) {
      var value = ""
      values.foreach(v => {
        if (isEmpty(value)) value = s"$columnName=$v"
        else value += s" or $columnName=$v"
      })
      value
    } else "1=1"
  }

  def sqlToWhereOr(columnName: String, values: String*): String = {
    if (nonEmpty(columnName) && nonEmpty(values)) {
      var value = ""
      values.foreach(v => {
        if (isEmpty(value)) value = s"$columnName='$v'"
        else value += s" or $columnName='$v'"
      })
      value
    } else "1=1"
  }

  def nonNull(columnNames: Seq[String]): String = {
    var value = ""
    if (nonEmpty(columnNames)) {
      columnNames.foreach(columnName => {
        if (isEmpty(value)) value = s"$columnName is not null"
        else value += s" and $columnName is not null "
      })
      value
    } else "1=1"
  }

}